home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / DrawSprocket / GoggleSprocket / GoggleSprocketTest Sources / GSpTest_DSpSupport.cp < prev    next >
Encoding:
Text File  |  1998-03-12  |  7.7 KB  |  327 lines  |  [TEXT/CWIE]

  1. /*
  2. ********************************************************************************
  3. **
  4. ** Name: GSpTest_DSpSupport.cp
  5. **
  6. ** Description:
  7. **
  8. **    Support routines for using DrawSprocket.
  9. **
  10. ********************************************************************************
  11. */
  12. #include "DrawSprocket.h"
  13.  
  14. #include "GSpTest_DSpSupport.h"
  15. #include "GSpTest_Main.h"
  16.  
  17. /*
  18. ********************************************************************************
  19. ** constants
  20. ********************************************************************************
  21. */
  22. #define kDisplayWidth        640
  23. #define kDisplayHeight        480
  24. #define kDisplayDepth        8
  25. #define kDisplayPages        2
  26.  
  27. /*
  28. ********************************************************************************
  29. ** globals
  30. ********************************************************************************
  31. */
  32. DSpContextAttributes gDSpContextAttributes;
  33. DSpContextReference gDSpContext;
  34.  
  35. /*
  36. ********************************************************************************
  37. ** private globals
  38. ********************************************************************************
  39. */
  40. static Boolean gDSpStarted = false;
  41.  
  42. /*
  43. ********************************************************************************
  44. ** local prototypes
  45. ********************************************************************************
  46. */
  47. static void MyInitContextAttributes( DSpContextAttributes *inAttributes );
  48.  
  49. /*
  50. ********************************************************************************
  51. **
  52. ** Name: DisplayInit()
  53. **
  54. ** Description:
  55. **
  56. **    Find the best context to use and reserve it.
  57. **
  58. ********************************************************************************
  59. */
  60. OSStatus            // result code
  61. DisplayInit( void )
  62. {
  63.     OSStatus theError;
  64.     
  65.     // startup DrawSprocket
  66.     theError = DSpStartup();
  67.     if( theError )
  68.         return theError;
  69.     gDSpStarted = true;
  70.     
  71.     // find the best context
  72.     MyInitContextAttributes( &gDSpContextAttributes );
  73.     theError = DSpFindBestContext( &gDSpContextAttributes, &gDSpContext );
  74.     if( theError )
  75.         return theError;
  76.     
  77.     // reserve the context
  78.     theError = DSpContext_Reserve( gDSpContext, &gDSpContextAttributes );
  79.     if( theError )
  80.     {
  81.         gDSpContext = NULL;
  82.         return theError;
  83.     }
  84.  
  85.     return noErr;
  86. }
  87.  
  88. /*
  89. ********************************************************************************
  90. **
  91. ** Name: DisplayActivate()
  92. **
  93. ** Description:
  94. **
  95. **    Activate the display.
  96. **
  97. ********************************************************************************
  98. */
  99. OSStatus            // result code
  100. DisplayActivate( void )
  101. {
  102.     // make sure we have a context
  103.     if( NULL == gDSpContext )
  104.         return noErr;
  105.         
  106.     return DSpContext_SetState( gDSpContext, kDSpContextState_Active );
  107. }
  108.  
  109. /*
  110. ********************************************************************************
  111. **
  112. ** Name: DisplayPause()
  113. **
  114. ** Description:
  115. **
  116. **    Pause the display.
  117. **
  118. ********************************************************************************
  119. */
  120. OSStatus            // result code
  121. DisplayPause( void )
  122. {
  123.     DSpContextState theState;
  124.     OSStatus theError;
  125.     
  126.     // make sure we have a context
  127.     if( NULL == gDSpContext )
  128.         return noErr;
  129.         
  130.     // a context must be active before it can be paused
  131.     theError = DSpContext_GetState( gDSpContext, &theState );
  132.     if( theError )
  133.         return theError;
  134.     if( theState != kDSpContextState_Active )
  135.     {
  136.         theError = DisplayActivate();
  137.         if( theError )
  138.             return theError;
  139.     }
  140.         
  141.     return DSpContext_SetState( gDSpContext, kDSpContextState_Paused );
  142. }
  143.  
  144. /*
  145. ********************************************************************************
  146. **
  147. ** Name: DisplayFadeOut()
  148. **
  149. ** Description:
  150. **
  151. **    Fade the display out.
  152. **
  153. ********************************************************************************
  154. */
  155. OSStatus            // result code
  156. DisplayFadeOut( void )
  157. {
  158.     // make sure we have a context
  159.     if( NULL == gDSpContext )
  160.         return noErr;
  161.         
  162.     return DSpContext_FadeGammaOut( NULL, NULL );
  163. }
  164.  
  165. /*
  166. ********************************************************************************
  167. **
  168. ** Name: DisplayFadeIn()
  169. **
  170. ** Description:
  171. **
  172. **    Fade the display in.
  173. **
  174. ********************************************************************************
  175. */
  176. OSStatus            // result code
  177. DisplayFadeIn( void )
  178. {
  179.     // make sure we have a context
  180.     if( NULL == gDSpContext )
  181.         return noErr;
  182.         
  183.     return DSpContext_FadeGammaIn( NULL, NULL );
  184. }
  185.  
  186. /*
  187. ********************************************************************************
  188. **
  189. ** Name: DisplayRelease
  190. **
  191. ** Description:
  192. **
  193. **    Release the display.
  194. **
  195. ********************************************************************************
  196. */
  197. OSStatus            // result code
  198. DisplayRelease( void )
  199. {
  200.     OSStatus theError;
  201.  
  202.     // make sure we have a context
  203.     if( NULL == gDSpContext )
  204.         return noErr;
  205.         
  206.     // fade out the display
  207.     DisplayFadeOut();
  208.         
  209.     // inactivate it
  210.     DSpContext_SetState( gDSpContext, kDSpContextState_Inactive );
  211.     
  212.     // fade in the display
  213.     DisplayFadeIn();
  214.     
  215.     // release it
  216.     theError = DSpContext_Release( gDSpContext );
  217.     gDSpContext = NULL;
  218.  
  219.     // shutdown DrawSprocket
  220.     if( gDSpStarted )    
  221.     {
  222.         DSpShutdown();
  223.         gDSpStarted = false;
  224.     }
  225.         
  226.     return theError;
  227. }
  228.  
  229. /*
  230. ********************************************************************************
  231. **
  232. ** Name: DisplayGetBuffer
  233. **
  234. ** Description:
  235. **
  236. **    Get the current drawing buffer for the display.
  237. **
  238. ********************************************************************************
  239. */
  240. CGrafPtr
  241. DisplayGetBuffer( void )
  242. {
  243.     OSStatus theError;
  244.     CGrafPtr theBuffer = NULL;
  245.     
  246.     // get the buffer
  247.     theError = DSpContext_GetBackBuffer( gDSpContext, kDSpBufferKind_Normal,
  248.         &theBuffer );
  249.     if( theError )
  250.         HandleError( theError, true );
  251.     
  252.     return theBuffer;
  253. }
  254.  
  255. /*
  256. ********************************************************************************
  257. **
  258. ** Name: DisplayShowBuffer
  259. **
  260. ** Description:
  261. **
  262. **    Show the current drawing buffer for the display.
  263. **
  264. ********************************************************************************
  265. */
  266. OSStatus
  267. DisplayShowBuffer( void )
  268. {
  269.     return DSpContext_SwapBuffers( gDSpContext, NULL, NULL );
  270. }
  271.  
  272. /******************************************************************************/
  273. #pragma mark -
  274. #pragma mark ## static functions ##
  275.     
  276. /*
  277. ********************************************************************************
  278. **
  279. ** Name: MyInitContextAttributes
  280. **
  281. ** Description:
  282. **
  283. **    Initialize a context attributes structure so that there is no garbage
  284. **    data in it.  This is important to do because DS will return an error
  285. **    if some fields are set incorrectly (including the "reserved" fields not
  286. **    being set to zero), and some things can actually cause a crash (such
  287. **    as a bogus color table handle).
  288. **
  289. ********************************************************************************
  290. */
  291. static void
  292. MyInitContextAttributes(
  293.     DSpContextAttributes *inAttributes        /* attr structure to init */
  294. )
  295. {
  296.     // init the context attributes structure
  297.     inAttributes->frequency                    = 0;
  298.     inAttributes->displayWidth                = 0;
  299.     inAttributes->displayHeight                = 0;
  300.     inAttributes->reserved1                    = 0;
  301.     inAttributes->reserved2                    = 0;
  302.     inAttributes->colorNeeds                = 0;
  303.     inAttributes->colorTable                = NULL;
  304.     inAttributes->contextOptions            = 0;
  305.     inAttributes->backBufferDepthMask        = 0;
  306.     inAttributes->displayDepthMask            = 0;
  307.     inAttributes->backBufferBestDepth        = 0;
  308.     inAttributes->displayBestDepth            = 0;
  309.     inAttributes->pageCount                    = 0;
  310.     inAttributes->gameMustConfirmSwitch        = false;
  311.     inAttributes->reserved3[0]                = 0;
  312.     inAttributes->reserved3[1]                = 0;
  313.     inAttributes->reserved3[2]                = 0;
  314.     inAttributes->reserved3[3]                = 0;
  315.  
  316.     // override the values that I want to customize
  317.     inAttributes->displayWidth            = kDisplayWidth;
  318.     inAttributes->displayHeight            = kDisplayHeight;
  319.     inAttributes->colorNeeds            = kDSpColorNeeds_Require;
  320.     inAttributes->colorTable            = NULL;
  321.     inAttributes->backBufferDepthMask    = kDisplayDepth;
  322.     inAttributes->backBufferBestDepth    = kDisplayDepth;
  323.     inAttributes->displayDepthMask        = kDisplayDepth;
  324.     inAttributes->displayBestDepth        = kDisplayDepth;
  325.     inAttributes->pageCount                = kDisplayPages;
  326. }
  327.